<?php
/**
 * Core class of iLivGallery, give you everything you need to
 * open directories and parse for images.
 *
 * PHP 5
 *
 * MIT License
 * Copyright (c) 2009 Christian Krause
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @category   library
 * @package    iLivGallery
 * @author     Original Author <chris@christiankrause.de>
 * @copyright  2009 Christian Krause
 * @license    MIT
 * @link       http://code.google.com/p/ilivgallery/downloads/
 * @since      File available since Release 0.5
 */

class iLiv_Setup {

	public $cfgPath			= '';
	public $sysiLivRoot		= '';
	public $sysDocRoot		= '';
	public $sysGalleryPath  = '';

	public $sysConfig		= null;
	public $debugger		= null;

	public function __construct( iLiv_Debugger $debugger, iLiv_Config $config ) {
		$this->debugger =  $debugger;
		$this->sysConfig = $config;
		$this->setPath();
	}

	/**
	 * @return iLiv_Setup
	 */
	private function setPath() {

		//we use this because the "getcwd" function doesnt work if some parent directories aint got read rights
		$this->sysiLivRoot = realpath( dirname(__FILE__).
									   DIRECTORY_SEPARATOR.
									   '..'.
									   DIRECTORY_SEPARATOR );

		$this->sysDocRoot 	  = $_SERVER['DOCUMENT_ROOT'];

		$this->sysGalleryPath = $this->getGalleryRoot().
					 			DIRECTORY_SEPARATOR.
				 	  			$this->sysConfig->getValue('gallery_path');

		$this->cfgPath		  = $this->sysiLivRoot.DIRECTORY_SEPARATOR.'config'.
													DIRECTORY_SEPARATOR.'config.cfg';

		return $this;
	}

	/**
	 * @return string
	 */
	public function getGalleryRoot() {

		if ( $this->sysConfig->getValue('use_doc_root') )
		{
			return $this->sysDocRoot;
		}

		return $this->sysiLivRoot;
	}

	public function getCfgValueByKey($sKey) {
		return $this->sysConfig->getValue($sKey);
	}

	public function getLastCfgDate() {
		return (int) $this->sysConfig->getValue('time_stamp');
	}


	private function loadCfgFile() {

		if ( file_exists($this->cfgPath) )
		{
			$cfgHandle 	 = fopen($this->cfgPath, "r");
			$fileContent = fread($cfgHandle);

			$config = unserialize($fileContent);

			if ( is_object($config) )
			{
				$this->sysConfig = $config;
			}
			return true;
		}

		return false;
	}

	private function saveCfgFile() {

		$cfgHandle = fopen($this->cfgPath, "w");

		if ( fwrite($cfgHandle, serialize($this->sysConfig)) )
		{
			return true;
		}

		return false;
	}

}
